<?php

function InsertTripplanner()
{
    global $dbsql,$language;
    
    require_once "includes/php-captcha.inc.php";
    
    if (PhpCaptcha::Validate($_POST['SpamCode'])==false)
    {
         AjaxTransfer('spam',getLang('Please enter the correct security code!'));
    }
    
    
    foreach($_POST as $key => $vl)
    {
        $data[$key] = gpc_getStringPost($key);
    }
    
    $data['locations']  = implode(', ',gpc_getStringArray('locations'));
    $data['date']       = date('Y-m-d');
    $ip                 = $_SERVER['REMOTE_ADDR'];
    
    
    
    $sql_insert="insert into tbltripplanner
			(sex, cName, country,email,cAddress,cFax, phone, adults, adults_ages, children,children_ages, 
				arrival_date,arrival_port, departure_date,  departure_port,duration,destinations,
				activities,transportation, accommodation_food, questions_comments, findus,dDate,iStatus,cLanguage) 
				
		values('".$data['sex']."', '".$data['name']."', '".$data['country']."','".$data['email']."','".$data['address']."','".$data['fax']."', '".$data['phone']."', '".$data['adults']."', '".$data['adults_Ages']."', 
			'".$data['children']."', '".$data['children_Ages']."','".$data['arrival_date']."','".$data['arrival_port']."', '".$data['departure_date']."', '".$data['departure_port']."',
			 '".$data['duration']."', '".$data['locations']."','".$data['activities']."','".$data['transportation']."', '".$data['roomtype_details']." <br>Meals: ".$data['meal']."',
			 '".$data['comments']." <br> Ip: $ip','".$data['findus']."','".$data['date']."','0','".$language."')";
	//echo $sql_insert;
	$dbsql->query($sql_insert);
    
    
    
//send tripplanner to email:

$message="
    <b>I. ".__('Personal Details').":</b>
    <div style='padding-left:40'>
    	".__('Full name').": ".$data['sex']."  ".$data['name']."<br>
    	".__('Country').": ".$data['country']."<br>
    	".__('Email').": ".$data['email']."<br>
    	".__('Address').": ".$data['address']."<br>
    	".__('Fax').": ".$data['fax']."<br>
    	".__('Phone').": ".$data['phone']."<br>
    	</div><br><br>
    
    <b>II. ".__('Travel Period').":</b>
    <div style='padding-left:20'>
    	1. ".__('Departure').":</div>
    <div style='padding-left:40'>
    	".__('Num of Adults').": ".$data['adults']." , Ages: ".$data['adults_Ages']."<br>
    	".__('Num of child').": ".$data['children']." , Ages: ".$data['children_Ages']."<br>
    	".__('Arrivaldate').": ".$data['arrival_date'] ."
    	".__('Arrival port').": ".$data['arrival_port']."
    	".__('Departure date').": ".$data['departure_date'] ."
    	".__('Departure port').": ".$data['departure_port']."
    	".__('Duration').": ".$data['duration']."</div><br>
    	
    <div style='padding-left:20'>
    	2. ".__('Destination in Vietnam').":</div>
    <div style='padding-left:40'>
    	".$data['locations']."</div><br><br>
    
    <b>III. ".__('Activities').":</b>
    <div style='padding-left:40'>
    	".$data['activities']."</div><br><br>
    
    <b>IV. ".__('Transportation').":</b>
    <div style='padding-left:40'>
    	".$data['transportation']."</div><br><br>
    
    <b>V. ".__('Accommodation food').":</b>
    <div style='padding-left:40'>
    	".$data['roomtype_details']."
    	Meals: ".$data['meal']."</div><br><br>
    
    <b>VI. ".__('Comments').":</b>
    <div style='padding-left:40'>
    	".$data['comments']."</div><br><br>
    
    ".__('How find us?').": ".$data['findus']."<br>
    
    IP: ".$ip."";

    $emailconfig       = @getConfigEmail('TripplannerEmail');
    if($emailconfig==false)
    {
        //AjaxTransfer('config','Email not config');
    }
	$subject                = $emailconfig->title;
    $subject                = str_ireplace('{NAME}',$data['sex']." ".$data['name'],$subject);
    
    $emailconfig->content   = str_ireplace('{NAME}',$data['sex']." ".$data['name'],$emailconfig->content);
    $emailconfig->content   = str_ireplace('{email}',$data['email'],$emailconfig->content);
    $emailconfig->content   = str_ireplace('{message}',$message,$emailconfig->content);
    
 	//send to admin
 	$rs   = send_email($emailconfig->email,"",$subject,$message);
 	if($rs==false)
    {
        //AjaxTransfer('ok','Send Email False please contact to admin');
    }
 	//send to cus---------------------	    
	$rs   = send_email($Email,"",$subject,$emailconfig->content);     	
	if($rs==false)
    {
        //AjaxTransfer('ok','Send Email False please contact to admin');
    }
    	
	
	$datasuccess= "<table width='100%'><tr><td><b>".__('Thank you')." ".$data['sex']." ".$data['name']."!<b><br><br></td></tr>
	<tr><td>".__('Your request sent')."</td></tr></table>";
    
	AjaxTransfer('ok',$datasuccess);
}